1.5. Creating a Web Application that can be deployed on Heroku 创建能部署在Heroku上面的Web项目

1.4 节类似的,创建一个Web项目打包成 WAR 部署在 Servlet 容器或者发布到Heroku。执行下面命令

  1. mvn archetype:generate -DarchetypeArtifactId=jersey-heroku-webapp \
  2. -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
  3. -DgroupId=com.example -DartifactId=simple-heroku-webapp -Dpackage=com.example \
  4. -DarchetypeVersion=2.16

在你的项目里面随意调整pom.xml内的groupId,包号和版本号就可以成为一个新的项目。

此时,simple-heroku-webapp已经创建, 符合Maven的项目结构:

  • 标准的管理配置文件 :pom.xml
  • 原文件路径 :src/main/java
  • 资源文件路径 :src/main/resources
  • web应用文件 :src/main/webapp
  • 原文件测试(基于JerseyTest) :src/test/java
  • Heroku系统属性 (OpenJDK版本) :system.properties
  • Heroku 应用的 进程类型列表 : Procfile

该项目包含一个JAX-RS资源类 MyResouce,和一个资源的方法会返回的简单文本。确保资源的正确测试,MyResourceTest 是一个端到端的测试案例(测试是基于JerseyTest,详见 Chapter 24, Jersey Test Framework)。类似simple-service-webapp项目,在 src/main/webapp/WEB-INF 下,它包含了标准的JavaEE Web 应用的 web.xml 部署描述符,目标是部署在一个 Servlet 容器(本例将运行在 Heroku 的 Jetty 容器)。

项目打包成WAR,执行:

  1. mvn clean package

打包成功,如下:

  1. Results :
  2. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  3. [INFO]
  4. [INFO] --- maven-war-plugin:2.2:war (default-war) @ simple-heroku-webapp ---
  5. [INFO] Packaging webapp
  6. [INFO] Assembling webapp [simple-heroku-webapp] in [D:\workspaceGithub\Jersey-2.
  7. x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\simple-heroku-webapp]
  8. [INFO] Processing war project
  9. [INFO] Copying webapp resources [D:\workspaceGithub\Jersey-2.x-User-Guide-Demos\
  10. demo-1.5\simple-heroku-webapp\src\main\webapp]
  11. [INFO] Webapp assembled in [103 msecs]
  12. [INFO] Building war: D:\workspaceGithub\Jersey-2.x-User-Guide-Demos\demo-1.5\sim
  13. ple-heroku-webapp\target\simple-heroku-webapp.war
  14. [INFO] WEB-INF\web.xml already added, skipping
  15. [INFO]
  16. [INFO] --- maven-dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ s
  17. imple-heroku-webapp ---
  18. [INFO] Copying jersey-container-servlet-core-2.12.jar to D:\workspaceGithub\Jers
  19. ey-2.x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-c
  20. ontainer-servlet-core-2.12.jar
  21. [INFO] Copying hk2-api-2.3.0-b10.jar to D:\workspaceGithub\Jersey-2.x-User-Guide
  22. -Demos\demo-1.5\simple-heroku-webapp\target\dependency\hk2-api-2.3.0-b10.jar
  23. [INFO] Copying javassist-3.18.1-GA.jar to D:\workspaceGithub\Jersey-2.x-User-Gui
  24. de-Demos\demo-1.5\simple-heroku-webapp\target\dependency\javassist-3.18.1-GA.jar
  25. [INFO] Copying jetty-security-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2
  26. .x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-securi
  27. ty-9.0.6.v20130930.jar
  28. [INFO] Copying validation-api-1.1.0.Final.jar to D:\workspaceGithub\Jersey-2.x-U
  29. ser-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\validation-api-1
  30. .1.0.Final.jar
  31. [INFO] Copying jetty-webapp-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x
  32. -User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-webapp-9
  33. .0.6.v20130930.jar
  34. [INFO] Copying jersey-client-2.12.jar to D:\workspaceGithub\Jersey-2.x-User-Guid
  35. e-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-client-2.12.jar
  36. [INFO] Copying osgi-resource-locator-1.0.1.jar to D:\workspaceGithub\Jersey-2.x-
  37. User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\osgi-resource-l
  38. ocator-1.0.1.jar
  39. [INFO] Copying jersey-common-2.12.jar to D:\workspaceGithub\Jersey-2.x-User-Guid
  40. e-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-common-2.12.jar
  41. [INFO] Copying jersey-server-2.12.jar to D:\workspaceGithub\Jersey-2.x-User-Guid
  42. e-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-server-2.12.jar
  43. [INFO] Copying jetty-io-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x-Use
  44. r-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-io-9.0.6.v20
  45. 130930.jar
  46. [INFO] Copying jetty-server-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x
  47. -User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-server-9
  48. .0.6.v20130930.jar
  49. [INFO] Copying hk2-locator-2.3.0-b10.jar to D:\workspaceGithub\Jersey-2.x-User-G
  50. uide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\hk2-locator-2.3.0-b10
  51. .jar
  52. [INFO] Copying jetty-util-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x-U
  53. ser-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-util-9.0.6
  54. .v20130930.jar
  55. [INFO] Copying jetty-http-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x-U
  56. ser-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-http-9.0.6
  57. .v20130930.jar
  58. [INFO] Copying jersey-container-servlet-2.12.jar to D:\workspaceGithub\Jersey-2.
  59. x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-contai
  60. ner-servlet-2.12.jar
  61. [INFO] Copying hk2-utils-2.3.0-b10.jar to D:\workspaceGithub\Jersey-2.x-User-Gui
  62. de-Demos\demo-1.5\simple-heroku-webapp\target\dependency\hk2-utils-2.3.0-b10.jar
  63. [INFO] Copying jetty-xml-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.x-Us
  64. er-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-xml-9.0.6.v
  65. 20130930.jar
  66. [INFO] Copying javax.inject-2.3.0-b10.jar to D:\workspaceGithub\Jersey-2.x-User-
  67. Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\javax.inject-2.3.0-b
  68. 10.jar
  69. [INFO] Copying jersey-guava-2.12.jar to D:\workspaceGithub\Jersey-2.x-User-Guide
  70. -Demos\demo-1.5\simple-heroku-webapp\target\dependency\jersey-guava-2.12.jar
  71. [INFO] Copying aopalliance-repackaged-2.3.0-b10.jar to D:\workspaceGithub\Jersey
  72. -2.x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\aopallianc
  73. e-repackaged-2.3.0-b10.jar
  74. [INFO] Copying jetty-servlet-9.0.6.v20130930.jar to D:\workspaceGithub\Jersey-2.
  75. x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\jetty-servlet
  76. -9.0.6.v20130930.jar
  77. [INFO] Copying javax.annotation-api-1.2.jar to D:\workspaceGithub\Jersey-2.x-Use
  78. r-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\javax.annotation-a
  79. pi-1.2.jar
  80. [INFO] Copying javax.servlet-3.0.0.v201112011016.jar to D:\workspaceGithub\Jerse
  81. y-2.x-User-Guide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\javax.ser
  82. vlet-3.0.0.v201112011016.jar
  83. [INFO] Copying javax.ws.rs-api-2.0.1.jar to D:\workspaceGithub\Jersey-2.x-User-G
  84. uide-Demos\demo-1.5\simple-heroku-webapp\target\dependency\javax.ws.rs-api-2.0.1
  85. .jar
  86. [INFO] ------------------------------------------------------------------------
  87. [INFO] BUILD SUCCESS
  88. [INFO] ------------------------------------------------------------------------
  89. [INFO] Total time: 03:17 min
  90. [INFO] Finished at: 2014-08-30T10:17:48+08:00
  91. [INFO] Final Memory: 17M/42M
  92. [INFO] ------------------------------------------------------------------------

接下来你可以做:

  • 改变项目
  • 打包成 WAR 部署到任意 Servlet 容器
  • 或者部署到 Heroku(参见下文1.5.1)

提示: 可以执行mvn clean package jetty:run项目将会部署到内嵌的 Jetty 容器运行 ,或者执行java -cp target/classes:target/dependency/* com.example.heroku.Main(那是Jetty 在 Heroku 的启动方式)

1.5.1. Deploy it on Heroku 部署在Heroku

首先是要注册Heroku的账户,这里不展开讲。可以参考Getting Started with Java on Heroku。当你的Heroku环境准备完毕后,接着看下面的步骤:

首先给你的项目创建一个 Git 仓库:

  1. $ git init
  2. Initialized empty Git repository in /.../simple-heroku-webapp/.git/

接着创建 Heroku 的实例,并把远程引用添加到你的 Git 仓库:

  1. $ heroku create
  2. Creating simple-heroku-webapp... done, stack is cedar
  3. http://simple-heroku-webapp.herokuapp.com/ | git@heroku.com:simple-heroku-webapp.git
  4. Git remote heroku added

注意heroku create 默认创建的实例名称是一串随机的字符串类似与 tranquil-basin-4744,而不一定是你项目名simple-heroku-webapp。(译者注:当然你可以根据用户自定义实例名称,具体的要参考Getting Started with Java on Heroku)

添加并提交到你的 Git 仓库:

  1. $ git add src/ pom.xml Procfile system.properties
  2. $ git commit -a -m "initial commit"
  3. [master (root-commit) e2b58e3] initial commit
  4. 7 files changed, 221 insertions(+)
  5. create mode 100644 Procfile
  6. create mode 100644 pom.xml
  7. create mode 100644 src/main/java/com/example/MyResource.java
  8. create mode 100644 src/main/java/com/example/heroku/Main.java
  9. create mode 100644 src/main/webapp/WEB-INF/web.xml
  10. create mode 100644 src/test/java/com/example/MyResourceTest.java
  11. create mode 100644 system.properties

将修改推送到 Heroku:

  1. $ git push heroku master
  2. Counting objects: 21, done.
  3. Delta compression using up to 8 threads.
  4. Compressing objects: 100% (11/11), done.
  5. Writing objects: 100% (21/21), 3.73 KiB | 0 bytes/s, done.
  6. Total 21 (delta 0), reused 0 (delta 0)
  7. -----> Java app detected
  8. -----> Installing OpenJDK 1.7... done
  9. -----> Installing Maven 3.0.3... done
  10. -----> Installing settings.xml... done
  11. -----> executing /app/tmp/cache/.maven/bin/mvn -B -Duser.home=/tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd -Dmaven.repo.local=/app/tmp/cache/.m2/repository -s /app/tmp/cache/.m2/settings.xml -DskipTests=true clean install
  12. [INFO] Scanning for projects...
  13. [INFO]
  14. [INFO] ------------------------------------------------------------------------
  15. [INFO] Building simple-heroku-webapp 1.0-SNAPSHOT
  16. [INFO] ------------------------------------------------------------------------
  17. [INFO]
  18. [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ simple-heroku-webapp ---
  19. [INFO]
  20. [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ simple-heroku-webapp ---
  21. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  22. [INFO] skip non existing resourceDirectory /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/src/main/resources
  23. [INFO]
  24. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simple-heroku-webapp ---
  25. [INFO] Compiling 2 source files to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/classes
  26. [INFO]
  27. [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ simple-heroku-webapp ---
  28. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  29. [INFO] skip non existing resourceDirectory /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/src/test/resources
  30. [INFO]
  31. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ simple-heroku-webapp ---
  32. [INFO] Compiling 1 source file to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/test-classes
  33. [INFO]
  34. [INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ simple-heroku-webapp ---
  35. [INFO] Tests are skipped.
  36. [INFO]
  37. [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ simple-heroku-webapp ---
  38. [INFO] Packaging webapp
  39. [INFO] Assembling webapp [simple-heroku-webapp] in [/tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/simple-heroku-webapp]
  40. [INFO] Processing war project
  41. [INFO] Copying webapp resources [/tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/src/main/webapp]
  42. [INFO] Webapp assembled in [88 msecs]
  43. [INFO] Building war: /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/simple-heroku-webapp.war
  44. [INFO] WEB-INF/web.xml already added, skipping
  45. [INFO]
  46. [INFO] --- maven-dependency-plugin:2.1:copy-dependencies (copy-dependencies) @ simple-heroku-webapp ---
  47. [INFO] Copying guava-14.0.1.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/guava-14.0.1.jar
  48. [INFO] Copying javax.annotation-api-1.2.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/javax.annotation-api-1.2.jar
  49. [INFO] Copying validation-api-1.1.0.Final.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/validation-api-1.1.0.Final.jar
  50. [INFO] Copying javax.ws.rs-api-2.0.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/javax.ws.rs-api-2.0.jar
  51. [INFO] Copying jetty-http-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-http-9.0.6.v20130930.jar
  52. [INFO] Copying jetty-io-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-io-9.0.6.v20130930.jar
  53. [INFO] Copying jetty-security-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-security-9.0.6.v20130930.jar
  54. [INFO] Copying jetty-server-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-server-9.0.6.v20130930.jar
  55. [INFO] Copying jetty-servlet-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-servlet-9.0.6.v20130930.jar
  56. [INFO] Copying jetty-util-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-util-9.0.6.v20130930.jar
  57. [INFO] Copying jetty-webapp-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-webapp-9.0.6.v20130930.jar
  58. [INFO] Copying jetty-xml-9.0.6.v20130930.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jetty-xml-9.0.6.v20130930.jar
  59. [INFO] Copying javax.servlet-3.0.0.v201112011016.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/javax.servlet-3.0.0.v201112011016.jar
  60. [INFO] Copying hk2-api-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/hk2-api-2.2.0-b21.jar
  61. [INFO] Copying hk2-locator-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/hk2-locator-2.2.0-b21.jar
  62. [INFO] Copying hk2-utils-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/hk2-utils-2.2.0-b21.jar
  63. [INFO] Copying osgi-resource-locator-1.0.1.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/osgi-resource-locator-1.0.1.jar
  64. [INFO] Copying asm-all-repackaged-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/asm-all-repackaged-2.2.0-b21.jar
  65. [INFO] Copying cglib-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/cglib-2.2.0-b21.jar
  66. [INFO] Copying javax.inject-2.2.0-b21.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/javax.inject-2.2.0-b21.jar
  67. [INFO] Copying jersey-container-servlet-2.5.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jersey-container-servlet-2.5.jar
  68. [INFO] Copying jersey-container-servlet-core-2.5.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jersey-container-servlet-core-2.5.jar
  69. [INFO] Copying jersey-client-2.5.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jersey-client-2.5.jar
  70. [INFO] Copying jersey-common-2.5.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jersey-common-2.5.jar
  71. [INFO] Copying jersey-server-2.5.jar to /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/dependency/jersey-server-2.5.jar
  72. [INFO]
  73. [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ simple-heroku-webapp ---
  74. [INFO] Installing /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/target/simple-heroku-webapp.war to /app/tmp/cache/.m2/repository/com/example/simple-heroku-webapp/1.0-SNAPSHOT/simple-heroku-webapp-1.0-SNAPSHOT.war
  75. [INFO] Installing /tmp/build_992cc747-26d6-4800-bdb1-add47b9583cd/pom.xml to /app/tmp/cache/.m2/repository/com/example/simple-heroku-webapp/1.0-SNAPSHOT/simple-heroku-webapp-1.0-SNAPSHOT.pom
  76. [INFO] ------------------------------------------------------------------------
  77. [INFO] BUILD SUCCESS
  78. [INFO] ------------------------------------------------------------------------
  79. [INFO] Total time: 45.861s
  80. [INFO] Finished at: Mon Dec 09 19:51:34 UTC 2013
  81. [INFO] Final Memory: 17M/514M
  82. [INFO] ------------------------------------------------------------------------
  83. -----> Discovering process types
  84. Procfile declares types -> web
  85. -----> Compiled slug size: 75.9MB
  86. -----> Launching... done, v6
  87. http://simple-heroku-webapp.herokuapp.com deployed to Heroku
  88. To git@heroku.com:simple-heroku-webapp.git
  89. * [new branch] master -> master

现在你可以访问你的应用了。本例子是http://simple-heroku-webapp.herokuapp.com/myresource